/**
 * @description tabsBar标签页逻辑，如无必要请勿修改
 */
import { defineStore } from 'pinia'
import type { tabsState } from '@/types/tabs'
import routerApi from '@/service/api/router'
import { transformAuthRouteToVueRoutes } from '@/router/utils'
import router from '@/router'
export const useTabsStore = defineStore('tabs', {
  state: (): tabsState => ({
    locks: [],
    currrenName: '',
    visitedRoutes: [],
    levelOne: -1,
    oldRouter: []
  }),
  getters: {
    getCurrenName: (state: any) => state.currrenName,
    getLocaks: (state: any) => state.locks,
    getVisitedRoutes: (state: any) => state.visitedRoutes,
    getLeavelOne: state => state.levelOne,
    getHistory: (state: any) => {
      return state.visitedRoutes.filter((el: any) => {
        return !el.meta.isFarth
      })
    },
    getOneMeun: state => {
      return state.oldRouter.filter((el: any) => {
        return !el.meta.isPage
      })
    },
    getChilderInfo: state => {
      if (state.oldRouter.length > 0) {
        return state.oldRouter[state.levelOne]
      }
      return {
        meta: {},
        children: []
      }
    }
  },
  actions: {
    /**
     * @description 获取路由
     * @returns
     */
    async getRouter() {
      const { data } = await routerApi.roleList()
      const vueRoutes = transformAuthRouteToVueRoutes(data)
      this.oldRouter = vueRoutes
      vueRoutes.forEach((route: any) => {
        router.addRoute(route)
      })
    },
    /**
     * @description 获取一级菜单下标
     * @param to
     * @returns
     */
    getLeaverOne(to: any) {
      if (this.oldRouter.length > 0) {
        this.levelOne = this.oldRouter.findIndex((el: any) => el.childrenPathList.includes(to.path))
      } else {
        this.levelOne = 0
      }
    },
    /**
     * @description 设置下标
     * */
    setLeaverOne(index: number) {
      if (index === this.levelOne) return
      const pageInfo = this.oldRouter[index]
      this.levelOne = index
      return pageInfo
    },
    /**
     * @description 自动添加路由缓存
     * @param {*} from
     * @param {*} to
     * @returns
     */
    addVisitedRoute(from: any, to: any) {
      if (!to.meta.isPage) {
        this.setCurrenName(to.name)
        if (to.matched.length >= 3) {
          const farthInfo = JSON.parse(JSON.stringify(to.matched)).at(-2)
          farthInfo.meta.isFarth = true
          this.visitedRoutes = [
            {
              name: farthInfo.name,
              meta: farthInfo.meta
            },
            {
              name: to.name,
              meta: to.meta
            }
          ]
        } else {
          this.visitedRoutes = [
            {
              name: to.name,
              meta: to.meta
            }
          ]
        }
      } else {
        if (!this.currrenName) {
          const localName: any = localStorage.getItem('currenName')
          if (localName) {
            this.currrenName = localName
          }
        }
        const index = this.visitedRoutes.findIndex((el: any) => el.name === to.name)
        if (index >= 0) {
          this.visitedRoutes = this.visitedRoutes.filter((el: any, n: number) => {
            return n <= index
          })
        } else {
          this.visitedRoutes.push({
            name: to.name,
            meta: to.meta
          })
        }
      }
    },
    setCurrenName(name: string) {
      localStorage.setItem('currenName', name)
      this.currrenName = name
    },
    /**
     * @description 锁定当前路由
     * @param {*} name
     * @param {*} leavelOne
     * @returns
     */
    lockRouter(name: string, leavelOne: number) {
      this.locks.push({
        name,
        leavelOne,
        currrenName: this.currrenName,
        childer: this.visitedRoutes
      })
      this.visitedRoutes = []
    },
    /**
     * @description 解锁分组
     * @param {*} index
     * @returns
     */
    unlock(index: number) {
      this.visitedRoutes = this.locks[index].childer
      this.locks.splice(index, 1)
    },
    /**
     * @description 获取返回路由
     * @param {*} index
     * @returns
     */
    backName(index = -1) {
      return this.visitedRoutes.at(index).name
    }
  }
})
